package com.ruoyi.jwxt.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jwxt.domain.DTO.JwxtLabDTO;
import com.ruoyi.jwxt.domain.PO.JwxtLab;
import com.ruoyi.jwxt.domain.PO.JwxtSemester;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jwxt.domain.DTO.JwxtSemesterDTO;
import com.ruoyi.jwxt.service.IJwxtSemesterService;

/**
 * 学期管理Controller
 */
@RestController
@RequestMapping("/jwxt/semester")
public class JwxtSemesterController extends BaseController
{
    @Autowired
    private IJwxtSemesterService jwxtSemesterService;

    /**
     * 查询学期管理列表
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:list')")
    @GetMapping("/list")
    public TableDataInfo list(JwxtSemesterDTO jwxtSemesterDTO)
    {
        startPage();
        List<JwxtSemester> listPO=jwxtSemesterService.select(jwxtSemesterService.convert(jwxtSemesterDTO));
        TableDataInfo dataInfo=getDataTable(listPO);
        TableDataInfo tableDataCopy=new TableDataInfo();
        BeanUtils.copyProperties(dataInfo,tableDataCopy,"row");
        List<JwxtSemesterDTO> listDTO=listPO.stream().map(jwxtSemesterService::convert).collect(Collectors.toList());
        tableDataCopy.setRows(listDTO);

        return tableDataCopy;
    }

    /**
     * 获取所有学期的列表
     */
    //@PreAuthorize("@ss.hasPermi('jwxt:semester:listAll')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        return success(jwxtSemesterService.select(jwxtSemesterService.convert( new JwxtSemesterDTO())));
    }

    // /**
    //  * 导出学期管理列表
    //  */
    // @PreAuthorize("@ss.hasPermi('jwxt:semester:export')")
    // @Log(title = "学期管理", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, JwxtSemesterDTO jwxtSemesterDTO)
    // {
    //     List<JwxtSemesterDTO> list = jwxtSemesterService.select(jwxtSemesterDTO);
    //     ExcelUtil<JwxtSemesterDTO> util = new ExcelUtil<JwxtSemesterDTO>(JwxtSemesterDTO.class);
    //     util.exportExcel(response, list, "学期管理数据");
    // }

    /**
     * 获取学期管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jwxtSemesterService.select(id));
    }

    /**
     * 新增学期管理
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:add')")
    @Log(title = "学期管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JwxtSemesterDTO jwxtSemesterDTO)
    {
        return toAjax(jwxtSemesterService.insert(jwxtSemesterService.convert(jwxtSemesterDTO)));
    }

    /**
     * 修改学期管理
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:edit')")
    @Log(title = "学期管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JwxtSemesterDTO jwxtSemesterDTO)
    {
        return toAjax(jwxtSemesterService.update(jwxtSemesterService.convert(jwxtSemesterDTO)));
    }

    /**
     * 删除学期管理
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:remove')")
    @Log(title = "学期管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jwxtSemesterService.delete(ids));
    }

    /**
     * 设置当前全局学期
     */
    @PreAuthorize("@ss.hasPermi('jwxt:semester:edit')")
    @GetMapping("/set")
    public AjaxResult setCurSemeter(@RequestParam String name)
    {
        //System.out.println(name);
        return toAjax(jwxtSemesterService.setCurSemester(name));
    }
}
